<?php

function search_get_users($before,$after,$query) {
	$where = '';
	$sort = 'DESC';
	if ($before != -1) {
		$sort = 'DESC';
		$where = ' AND user_id < '.$before.' ';
	} elseif ($after != -1) {
		$sort = 'ASC';
		$where = ' AND user_id > '.$after.' ';
	}
	$user_results = db_array('SELECT DISTINCT user_id,username,name FROM users WHERE ('.$query.') '.$where.' ORDER BY user_id ASC LIMIT 0,11');
	if ($sort == 'ASC') {
		$user_results = array_reverse($user_results);
	}
	return $user_results;
}

function search_get_statuses($before,$after,$query) {
	$where = '';
	$sort = 'DESC';
	if ($before != -1) {
		$sort = 'DESC';
		$where = ' AND stat_id < '.$before.' ';
	} elseif ($after != -1) {
		$sort = 'ASC';
		$where = ' AND stat_id > '.$after.' ';
	}
	$statuses = db_array('SELECT stat_id,stat_date,stat_txt,reply_stat,restat_from,user_id,username,name FROM status INNER JOIN users ON stat_user = user_id WHERE ('.$query.') '.$where.' ORDER BY stat_id '.$sort.' LIMIT 0,11'); // equal 11 okay to more
	if ($sort == 'ASC') {
		$statuses = array_reverse($statuses);
	}
	return extend_statuses($statuses);
}

if (defined('NO_DIRECT')) {
	if (isset($_GET['q'])) {
		$q = preg_replace('/\s+/',' ',$_GET['q']);
		$q = trim($q);
		$q = amankan($q);
		if (strlen($q) == 0) {
			// query kosong dafuq
		} else {
			$q_explode = explode(' ',$q);
			$q_explode = array_unique($q_explode);
			$query_user = '';
			$query_status = '';
			$search_for_user = true;
			$search_for_status = true;
			foreach($q_explode as $key => $chunk) {
				if ($chunk[0] == '#') {
					$chunk = str_replace('#','#|',$chunk);
					$search_for_user = false;
				} elseif ($chunk[0] == '@') {
					$chunk = substr($chunk,1);
					$search_for_status = false;
				}
				$query_user .= ' username LIKE "%'.$chunk.'%" OR name LIKE "%'.$chunk.'%" OR ';
				$query_status .= ' stat_txt LIKE "%'.$chunk.'%" OR ';
			}
			if (strlen($query_user) > 0 && strlen($query_user) > 0) {
				$query_user = substr($query_user,0,-3);
				$query_status = substr($query_status,0,-3);
				$users = array();
				$statuses = array();
				if (isset($_GET['user_before'])) {
					$users = search_get_users(intval($_GET['user_before']),-1,$query_user);
				} elseif (isset($_GET['user_after'])) {
					$users = search_get_users(-1,intval($_GET['user_after']),$query_user);
				} elseif (isset($_GET['status_before'])) {
					$statuses = search_get_statuses(intval($_GET['status_before']),-1,$query_status);
				} elseif (isset($_GET['status_after'])) {
					$statuses = search_get_statuses(-1,intval($_GET['status_after']),$query_status);
				} else {
					if ($search_for_user) {
						$users = search_get_users(-1,-1,$query_user);
					}
					if ($search_for_status) {
						$statuses = search_get_statuses(-1,-1,$query_status);
					}
				}
				if (isset($_GET['ajax'])) {
					header('Content-type: text/plain');
				} else {
					$post = get_posts($session['user']['user_id']);
					$following = get_followings($session['user']['user_id']);
					$follower = get_followers($session['user']['user_id']);
					foreach ($users as $user) {
						echo '@'.$user['username'].' '.$user['name'].'<br />';
					}
					foreach ($statuses as $status) {
						echo show_status($status['stat_txt']).'<br />';
					}
				}
			}
		}
	}
}
